package com.loan.psychicloan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loan.psychicloan.R;
import com.loan.psychicloan.activity.WebViewActivity;
import com.loan.psychicloan.bean.LoanListBean;
import com.loan.psychicloan.imageloder.ImageLoader;
import com.loan.psychicloan.imageloder.ImageLoaderUtil;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by watilion on 2017/12/19 0019.
 */

public class LoanAdapter extends RecyclerView.Adapter<LoanAdapter.LoanHolder>  {

    public List<LoanListBean.DataBean> mList;
    private Context context;
    DecimalFormat df = new DecimalFormat("#####0.00");
    ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    ImageLoader.Builder builder = new ImageLoader.Builder();
    ImageLoader imageLoader;

    public LoanAdapter(Context context, List<LoanListBean.DataBean> data){
        mList = data;
        this.context = context;

    }
    @Override
    public LoanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LoanHolder holder = new LoanHolder(LayoutInflater.from(context).
                inflate(R.layout.item_layout,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(LoanHolder holder, final int position) {

        imageLoader =builder.url(mList.get(position).loanlogo).placeHolder(R.mipmap.ic_launcher).imageView(holder.ivicon).build();
        imageLoaderUtil.loadImage(context,imageLoader);

        holder.tvTitle.setText(mList.get(position).loanname);
        holder.tvLoandes.setText(mList.get(position).loandes);
        if(mList.get(position).maxamount<10000){
            holder.tvMaxAmount.setText(String.valueOf(mList.get(position).maxamount));
            holder.tvMonetaryUnit.setText("元");
        }else{
            holder.tvMaxAmount.setText(df.format((double)mList.get(position).maxamount/10000));
            holder.tvMonetaryUnit.setText("万元");
        }
        if (mList.get(position).time<60){
            holder.tvMinTime.setText(String.valueOf(mList.get(position).time));
            holder.tvTimeUnit.setText("分钟");
        }else if (mList.get(position).time>=60 && mList.get(position).time<1440){
            holder.tvMinTime.setText(df.format((double)mList.get(position).time/60));
            holder.tvTimeUnit.setText("小时");
        }else{
            holder.tvMinTime.setText(df.format((double)mList.get(position).time/1440));
            holder.tvTimeUnit.setText("天");
        }
        holder.loanNowBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v){
                //Uri uri = Uri.parse(mList.get(position).loanurl);
                Intent mIntent = new Intent(context, WebViewActivity.class);
                mIntent.putExtra("loanurl",mList.get(position).loanurl);
                context.startActivity(mIntent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }



    class LoanHolder extends RecyclerView.ViewHolder{

        public ImageView ivicon;
        public TextView tvTitle,tvMaxAmount,tvMinTime,tvLoandes,tvMonetaryUnit,tvTimeUnit;
        public Button loanNowBtn;
        public LoanHolder(View itemView){
            super(itemView);
            ivicon = itemView.findViewById(R.id.iv_icon);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvMaxAmount = itemView.findViewById(R.id.tv_maxamount);
            tvMinTime = itemView.findViewById(R.id.tv_mintime);
            tvLoandes = itemView.findViewById(R.id.tv_loandes);
            tvMonetaryUnit = itemView.findViewById(R.id.tv_monetary_unit);
            tvTimeUnit = itemView.findViewById(R.id.tv_time_unit);
            loanNowBtn = itemView.findViewById(R.id.loan_now_btn);

        }
    }
}
